package com.augurit.wf.bi.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.dozer.DozerBeanMapper;
import org.hibernate.SQLQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import com.augurit.common.util.HqlUtils;
import com.augurit.common.util.PageUtils;
import com.augurit.common.util.lang.StringUtils;
import com.augurit.wf.bi.convert.WfBusInstanceConvertor;
import com.augurit.wf.bi.service.IWfBusInstanceService;
import com.augurit.wf.bi.web.form.WfBusInstanceForm;
import com.augurit.wf.client.summary.web.form.WfTaskSummaryForm;
import com.augurit.wf.common.dao.WfBusInstanceDao;
import com.augurit.wf.common.entity.WfBusInstance;
import com.augurit.wf.common.entity.WfBusTemplate;
import com.augurit.wf.common.entity.WfBusTemplateType;
import com.augurit.wf.common.form.WfBusBaseEntity;
import com.augurit.wf.common.util.WfUtils;

@Service
@Transactional
public class WfBusInstanceServiceImpl implements IWfBusInstanceService{

	@Autowired
	private WfBusInstanceDao wfBusInstanceDao;
	
	@Autowired
	private DozerBeanMapper dozer;
	
	/**
	 * 根据主键获取Form对象
	 */
	@Transactional(readOnly = true)
	public WfBusInstanceForm get(Long id) {
		WfBusInstance entity = wfBusInstanceDao.get(id);
		return WfBusInstanceConvertor.convertVoToForm(entity);
	}
	
	/**
	 * 删除Form对象列表
	 */
	public void delete(Long... ids) {
		wfBusInstanceDao.delete(ids);
	}

	/**
	 * 保存新增或修改的Form对象列表
	 */
	public void save(WfBusInstanceForm... forms) {
		if(forms != null)
			for(WfBusInstanceForm form : forms)
				this.save(form);
	}
	
	/**
	 * 保存新增或修改的Form对象
	 */
	public void save(WfBusInstanceForm form){
		
		if(form != null){
			WfBusInstance entity = null;
			
			//准备VO对象
			if(form != null && form.getWfBusInstanceId() != null){
				entity = wfBusInstanceDao.get(form.getWfBusInstanceId());
			}else{
				entity = new WfBusInstance();
			}
			
			//属性值转换
			WfBusInstanceConvertor.convertFormToVo(form, entity);
			
			//保存
			wfBusInstanceDao.save(entity);
			
			//回填ID属性值
			form.setWfBusInstanceId(entity.getWfBusInstanceId());
		}
	}

	/**
	 * 根据Form对象的查询条件作分页查询
	 */
	@Transactional(readOnly = true)
	public Page<WfBusInstanceForm> search(Page<WfBusInstanceForm> page, WfBusInstanceForm form) {
		// 建立新的分页对象
		Page pg = PageUtils.newPageInstance(page);
		
		// 查询语句及参数
		StringBuffer hql = new StringBuffer("from WfBusInstance ps where 1=1");
		List values = new ArrayList();
		
		// 查询条件
		if(form != null){
			
		}
		
		//排序
		hql.append(HqlUtils.buildOrderBy(page, "ps"));
		
		// 执行分页查询操作
		wfBusInstanceDao.findPage(pg, hql.toString(), values);
		
		// 转换为Form对象列表并赋值到原分页对象中
		List<WfBusInstanceForm> list = WfBusInstanceConvertor.convertVoListToFormList(pg.getResult());
		PageUtils.copy(pg, list, page);
		return page;
	}
	
	/**
	 * 根据过滤条件列表作分页查询
	 */
	@Transactional(readOnly = true)
	public Page<WfBusInstanceForm> search(Page<WfBusInstanceForm> page, List<PropertyFilter> filters) {
		// 建立新的分页对象
		Page<WfBusInstance> pg = PageUtils.newPageInstance(page);
		
		// 按过滤条件分页查找对象
		wfBusInstanceDao.findPage(pg, filters);
		
		// 转换为Form对象列表并赋值到原分页对象中
		List<WfBusInstanceForm> list = WfBusInstanceConvertor.convertVoListToFormList(pg.getResult());
		PageUtils.copy(pg, list, page);
		return page;
	}
	
	
	@Transactional(readOnly = true)
	public WfBusInstanceForm getByWfProcInstId(String wfInstanceId){
		if(wfInstanceId != null && wfInstanceId.trim().length() > 0){
			//String hql = "select inst, template, type from WfBusInstance inst, WfBusTemplate template, WfBusTemplateType type where inst.templateCode=template.templateCode and template.templateTypeId=type.id and inst.wfInstanceId=?";
			String hql = "select inst, template, type from WfBusInstance inst, WfBusTemplate template, WfBusTemplateType type where inst.templateId=template.id and template.templateTypeId=type.id and inst.procInstId=?";
			List<Object[]> list = wfBusInstanceDao.find(hql, wfInstanceId);
			if(list != null && list.size() > 0){
				WfBusInstance inst = (WfBusInstance)list.get(0)[0];
				WfBusTemplate template = (WfBusTemplate)list.get(0)[1];
				WfBusTemplateType type = (WfBusTemplateType)list.get(0)[2];
				
				WfBusInstanceForm result = WfBusInstanceConvertor.convertVoToForm(inst);
				result.setTemplateName(template.getTemplateName());
//				result.setWfDefKey(template.getWfDefKey());
				result.setTemplateTypeName(type.getTemplateTypeName());

				return result;
			}
		}
		return null;
	}
	
	/**
	 * 根据流程实例id删除
	 * @param templateCode
	 */
	public void deleteByProcInstId(String procInstId){
		this.wfBusInstanceDao.deleteByProcInstId(procInstId);
	}
	
	/**
	 * 同步业务摘要信息
	 * @param masterEntity
	 * @param masterEntityKey
	 */
//	public void syncSummaryForSql(final String masterEntity, final String masterEntityKey){
//		WfBusInstance entity = getByMasterEntityAndKey(masterEntity, masterEntityKey);
//		
//		if(entity != null){
//			
//			String sql = "select bus.* from " + masterEntity + " bus where bus.id=?";
//
//			SQLQuery query = wfBusInstanceDao.getSession().createSQLQuery(sql);
//			query.setString(0, masterEntityKey);
//			Object[] objs = (Object[])query.uniqueResult();
//			
//			
//			//默认的复制业务字段至流程汇总表策略
//			WfUtils.convertToTaskSummary(objs, entity); 
//			
//			//TODO: 自定义复制业务字段至流程汇总表策略
//			
//			
//			wfBusInstanceDao.save(entity);
//		}
//	}
	
	/**
	 * 同步主业务实体信息到汇总字段中
	 * @param <T>
	 * @param masterEntity
	 * @param masterEntityKey
	 * @param form
	 */
	public <T extends WfBusBaseEntity> void syncSummary(final String masterEntity, final String masterEntityKey, final T form){
		WfBusInstance entity = this.getByMasterEntityAndKey(masterEntity, masterEntityKey);
		if(entity != null){
			WfBusInstanceForm wfBusInstanceForm = WfBusInstanceConvertor.convertVoToForm(entity);
			dozer.map(form, wfBusInstanceForm);
			if(StringUtils.isNotBlank(wfBusInstanceForm.getBusMemo1())){
				this.save(wfBusInstanceForm);
			}
			
		}
	}
	
	public WfBusInstance getByMasterEntityAndKey(final String masterEntity, final String masterEntityKey){
		if(masterEntity != null && masterEntity.trim().length() > 0 
				&& masterEntityKey != null && masterEntityKey.trim().length() > 0){
			
			String hql = "from WfBusInstance ps where ps.masterEntity=? and ps.masterEntityKey=?";
			return wfBusInstanceDao.findUnique(hql, masterEntity, masterEntityKey);
		}else
			return null;
	}

	public WfBusInstanceForm getInstanceByTaskId(String taskid) {
		
		
		
		return null;
	}

	public WfBusInstance getByProcInstId(String procInsId) {
		String hql=" from WfBusInstance where procInstId='"+procInsId+"'";
		return (WfBusInstance) this.wfBusInstanceDao.find(hql).get(0);
	}
}
